import { isFunction } from '../plugins/utils'
const vmDialog = weex.requireModule('vmDialog')

const Dialog = Object.create(null)
Dialog.install = Vue => {
  Vue.prototype.$dialog = {
    show(options = {}) {
      vmDialog.show(
        {
          title: options.title,
          content: options.content,
          positiveText: options.positiveText,
          negativeText: options.negativeText,
          // icon: options.icon,
          cancelOnTouchOutside: options.cancelOnTouchOutside,
          cancelable: options.cancelable
        },
        // 确定回调
        params => {
          if (isFunction(options.onPositive)) {
            options.onPositive(params)
          }
        },
        // 取消回调
        params => {
          if (isFunction(options.onNegative)) {
            options.onNegative(params)
          }
        },
        // 弹窗关闭回调
        params => {
          if (isFunction(options.onDismiss)) {
            options.onDismiss(params)
          }
        }
      )
    },
    list(options = {}) {
      const { onPositive, onDismiss, ...rest } = options
      vmDialog.list(
        rest,
        params => {
          if (isFunction(onPositive)) {
            onPositive(params)
          }
        },
        params => {
          if (isFunction(onDismiss)) {
            onDismiss(params)
          }
        }
      )
    },
    radio(options = {}) {
      const { onPositive, onNegative, onDismiss, ...rest } = options
      vmDialog.radio(
        rest,
        // 确定回调
        params => {
          if (isFunction(onPositive)) {
            onPositive(params)
          }
        },
        // 取消回调
        params => {
          if (isFunction(onNegative)) {
            onNegative(params)
          }
        },
        // 弹窗关闭回调
        params => {
          if (isFunction(onDismiss)) {
            onDismiss(params)
          }
        }
      )
    },
    select(options = {}) {
      const { onPositive, onNegative, onDismiss, ...rest } = options
      vmDialog.select(
        rest,
        // 确定回调
        params => {
          if (isFunction(onPositive)) {
            onPositive(params)
          }
        },
        // 取消回调
        params => {
          if (isFunction(onNegative)) {
            onNegative(params)
          }
        },
        // 弹窗关闭回调
        params => {
          if (isFunction(onDismiss)) {
            onDismiss(params)
          }
        }
      )
    },
    dateRange(options = {}) {
      const { onPositive, onReset, onDismiss, ...rest } = options
      vmDialog.dateRange(
        rest,
        // 确定回调
        params => {
          if (isFunction(onPositive)) {
            onPositive(params)
          }
        },
        // 取消回调
        params => {
          if (isFunction(onDismiss)) {
            onDismiss(params)
          }
        },
        // 重置
        params => {
          if (isFunction(onReset)) {
            onReset(params)
          }
        }
      )
    },
    datePick({ start, end, initialDate, type, onPositive, onDismiss } = {}) {
      // type: 0 'YearMonthDay' 1 'YearMonth' 2 'MonthDay'
      vmDialog.datePick(
        { start, end, initialDate, type },
        // 确定回调
        params => {
          if (isFunction(onPositive)) {
            onPositive(params)
          }
        },
        // 取消回调
        params => {
          if (isFunction(onDismiss)) {
            onDismiss(params)
          }
        }
      )
    }
  }
}

Vue.use(Dialog)
